package com.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.myapplication.model.TopPageGoods;
import com.myapplication.zhou.alldemo.GoodsInfoActivity;
import com.myapplication.zhou.alldemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/28.
 */
public class TopGridRecycleViewAdapter extends RecyclerView.Adapter<TopGridRecycleViewAdapter.MyViewHolder> {
    private List<TopPageGoods> navList = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private BitmapRequestBuilder<GlideUrl, Bitmap> requestBuilder;


    public TopGridRecycleViewAdapter(Context context, List<TopPageGoods> navList) {
        this.context = context;
        this.navList = navList;
        inflater = LayoutInflater.from(context);
        requestBuilder = Glide.with(context)
                .from(GlideUrl.class)
                .asBitmap()
                .dontAnimate()
                .centerCrop()
                .error(R.mipmap.wutu)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.top_grid_recycle_view, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view, topGridListener);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //   Glide.with(context).load(navList.get(position).getGoods_image()).error(R.mipmap.wutu).into(holder.imageView1);
        requestBuilder.load(new GlideUrl(navList.get(position).getGoods_image())).into(holder.imageView1);
        holder.textview.setText(navList.get(position).getGoods_name());
        holder.bindView(navList.get(position));
    }

    public void update(List<TopPageGoods> list) {
        this.navList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return navList.size();
    }

    TopGridListener topGridListener = new TopGridListener() {
        @Override
        public void onClick(View view, TopPageGoods nav, int position) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("goods_id",nav.getGoods_id());
            intent.putExtras(bundle);
            intent.setClass(context, GoodsInfoActivity.class);
            context.startActivity(intent);
        }
    };

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imageView1;
        private ImageView imageView2;
        private TextView textview;
        private TopGridListener topGridListener;

        public MyViewHolder(View itemView, TopGridListener topGridListener) {
            super(itemView);
            this.textview = (TextView) itemView.findViewById(R.id.textview);
            this.imageView2 = (ImageView) itemView.findViewById(R.id.imageView2);
            this.imageView1 = (ImageView) itemView.findViewById(R.id.imageView1);
            this.topGridListener = topGridListener;
            textview.setOnClickListener(this);
            imageView1.setOnClickListener(this);
            imageView2.setOnClickListener(this);
        }

        public void bindView(TopPageGoods nav) {
            textview.setTag(R.id.textview, nav);
            imageView2.setTag(R.id.imageView2, nav);
            imageView1.setTag(R.id.imageView1, nav);
        }


        @Override
        public void onClick(View v) {
            topGridListener.onClick(v, (TopPageGoods) v.getTag(v.getId()), getLayoutPosition());
        }
    }

    public interface TopGridListener {
        void onClick(View view, TopPageGoods nav, int position);
    }
}
